package org.vt.eldercare.serial;

import java.io.IOException;
import java.io.OutputStream;

public class SerialWriter {
	public static byte MIN_SAMPLE_RATE = 1;
	public static byte MAX_SAMPLE_RATE = 30;
	public static byte MAX_THRESHOLD_VALUE = 90;
	public static byte MIN_THRESHOLD_VALUE = 15;
	public static byte THRESHOLD_INCREMENT = 15;
	public static byte MAX_WAITTIME = 30;
	public static byte MIN_WAITTIME = 1;

	public static byte ENABLE_GYROSCOPE = 32;
	public static byte DISABLE_GYROSCOPE = 31;
	public static byte ENABLE_ACCELEROMETER = 33;
	public static byte DISABLE_ACCELEROMETER = 34;
	public static byte ENABLE_GYRO_X = 35;
	public static byte DISABLE_GYRO_X = 36;
	public static byte ENABLE_GYRO_Y = 37;
	public static byte DISABLE_GYRO_Y = 38;
	public static byte ENABLE_GYRO_Z = 39;
	public static byte DISABLE_GYRO_Z = 40;
	public static byte ENABLE_ACCELEROMETER_X = 41;
	public static byte DISABLE_ACCELEROMETER_X = 42;
	public static byte ENABLE_ACCELEROMETER_Y = 43;
	public static byte DISABLE_ACCELEROMETER_Y = 44;
	public static byte ENABLE_ACCELEROMETER_Z = 45;
	public static byte DISABLE_ACCELEROMETER_Z = 46;
	public static byte ENABLE_INACTIVITY_DETECT = 47;
	public static byte DISABLE_INACTIVITY_DETECT = 48;
	
	private static byte END_BYTE = (byte) 255;
	public static byte SEND_ECHO = (byte) 254;
	@SuppressWarnings("unused")
	private static byte ACK_BYTE = (byte) 200;

	// Uses commands 1 - 30
	public static byte SAMPLE_RATE(int rate) {
		if (rate > MAX_SAMPLE_RATE) {
			return MAX_SAMPLE_RATE;
		} else if (rate < MIN_SAMPLE_RATE) {
			return MIN_SAMPLE_RATE;
		}
		return (byte) rate;
	}

	// Uses commands 49 - 54
	public static byte INACTIVITY_DETECT_THRESHOLD(int threshold) {
		if (threshold > MAX_THRESHOLD_VALUE) {
			return (byte) (48 + MAX_THRESHOLD_VALUE / THRESHOLD_INCREMENT);
		} else if (threshold < MIN_THRESHOLD_VALUE) {
			return (byte) (48 + MIN_THRESHOLD_VALUE / THRESHOLD_INCREMENT);
		}
		return (byte) (48 + threshold / THRESHOLD_INCREMENT);
	}

	// Uses commands 55 - 84
	public static byte INACTIVITY_DETECT_WAITTIME(int seconds) {
		if (seconds > MAX_WAITTIME) {
			return (byte) (54 + MAX_WAITTIME);
		} else if (seconds < MIN_WAITTIME) {
			return (byte) (54 + MIN_WAITTIME);
		}
		return (byte) (54 + seconds);
	}

	/**
	 * @see org.vt.eldercare.serial.TwoWaySerialComm.serialWrite
	 */
	public static void serialWrite(final TwoWaySerialComm usbComm,
			final int nodeID, final byte command) {
		Thread t = new Thread(new Runnable() {
			public void run() {
				if (usbComm.isConnected()) {
					OutputStream outputStream = null;
					try {
						outputStream = usbComm.getOutputStream();
						byte[] messageBytes = { (byte) nodeID, command,
								END_BYTE };
						outputStream.write(messageBytes);

					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});
		t.start();
	}

	/**
	 * @see org.vt.eldercare.serial.TwoWaySerialComm.serialWrite
	 */
	public static void serialWrite(final TwoWaySerialComm usbComm,
			final int nodeID, final byte[] commands) {
		Thread t = new Thread(new Runnable() {
			public void run() {
				if (usbComm.isConnected()) {
					OutputStream outputStream = null;
					try {
						outputStream = usbComm.getOutputStream();
						byte[] messageBytes = { (byte) nodeID };
						outputStream.write(messageBytes);
						outputStream.write(commands);
						byte[] endByte = { END_BYTE };
						outputStream.write(endByte);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});
		t.start();
	}
}
